﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.IO;
using System.Net;
using System.Net.Mail;

public partial class test : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            try
            {
                var deal = new DealesDataContext();
                var City = deal.City_ReturnForSubscribe();
                foreach (var item in City)
                {
                    var EmailList = deal.Select_Subscribe(Convert.ToInt32(item.CityID));
                    var sub = new DealesDataContext();
                    var query1 = sub.Select_DealByCityIDNew(Convert.ToInt32(item.CityID));
                    int DealID = 0;
                    foreach (var item1 in query1)
                    {
                        DealID = Convert.ToInt32(item1.DealID);
                    }
                    foreach (var items in EmailList)
                    {
                        WebRequest objRequest = System.Net.HttpWebRequest.Create("http://www.zupons.com/Email.aspx?DID=" + DealID);
                        StreamReader sr = new StreamReader(objRequest.GetResponse().GetResponseStream());
                        string result = sr.ReadToEnd();
                        sr.Close();
                        string html = result;
                        //System.Collections.Specialized.ListDictionary replacements = new System.Collections.Specialized.ListDictionary();
                        //replacements.Add("<%MailSubject%>", html.ToString());
                        //System.Net.Mail.MailMessage confirmation = Mailer.CreateMessage(new System.Web.UI.Control() as WebControl,
                        //    Mailer.MessageTemplate.SubscribeUser, replacements, items.EmailID.ToString(), "Daily Subscride User");
                        //Mailer.InternalSend(confirmation, false);

                        MailMessage Mail = new MailMessage();
                        MailAddress ma = new MailAddress("mail@zupons.com", "Zupons");
                        Mail.From = ma;
                        Mail.To.Add(items.EmailID.ToString());
                        Mail.Subject = "Daily Subscride User";
                        Mail.Body = html;
                        Mail.IsBodyHtml = true;
                        try
                        {
                            SmtpClient smtpMailObj = new SmtpClient();
                            smtpMailObj.Host = "mail.zupons.com";
                            smtpMailObj.Send(Mail);
                            Response.Write("Your Message has been sent successfully");
                        }
                        catch (Exception ex)
                        {
                            Response.Write("Message Delivery Fails");
                        }
                    }

                }
            }
            catch
            {
            }
        }
    }
    protected void btnEmailSend_Click(object sender, EventArgs e)
    {

    }
}
